import fastifyCookie from '@fastify/cookie';
import fastifyCors from '@fastify/cors';
import { fastify } from 'fastify';
import { bootstrap } from 'fastify-decorators';
import path from 'path';
import pino from 'pino';
import pretty from 'pino-pretty';
import jwt from './plugins/jwt';
import err from './plugins/err';
import response from './plugins/response';
import middie from '@fastify/middie';
import prisma from './plugins/prisma';
import fastifyCacheman from 'fastify-cacheman';
import cache from './plugins/cache';
import fastifyStatic from '@fastify/static';
import { PUBLIC_PATH } from './config/env';
import scheduler from './plugins/scheduler';

const logger = pino(
    { level: 'info' },
    pino.multistream([
        pino.transport({
            target: 'pino/file',
            options: { destination: './info.log' },
        }),
        pretty({ colorize: true })
    ])
);

export const app = fastify({ logger, requestTimeout: 10000 });
app.register(scheduler);
app.register(middie);
app.register(err);
app.register(response);
app.register(fastifyStatic, {root: PUBLIC_PATH, prefix: '/static/'});
app.register(fastifyCors, { origin: 'http://localhost:5173/', credentials: true });
app.register(fastifyCookie);
app.register(jwt);
app.register(bootstrap, {
    directory: path.join(__dirname, './controllers'),
    mask: /\.controller\./
});
app.register(prisma);
app.register(cache);
app.register(fastifyCacheman, { engine: 'memory' });


app.listen({ port: 3000, host: '0.0.0.0' }).then(() => {
    console.log(`Application is ready and listening on http://localhost:3000`)
    console.log(`Available routes:`)
    console.log(app.printRoutes())
});
